﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.ArticleManage
{
	public partial class RelativeLinkSelector : BasePage
	{
		private string _callbackFunction;
		/// <summary>
		/// 回调函数。
		/// </summary>
		public string CallbackFunction
		{
			get
			{
				if (string.IsNullOrEmpty(_callbackFunction))
					_callbackFunction = Request.QueryString["CallbackFunction"];
				if (string.IsNullOrEmpty(_callbackFunction))
					_callbackFunction = "top.opener.SetRelativeLink";
				return _callbackFunction;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
				this.DataBind();
		}

		protected void ModuleArticle1_SelectFinish(object src, EventArgs e)
		{
			if (!ModuleArticle1.IsSelected) return;

			string script = "<script type=\"text/javascript\">\n";
			script += "<!--\n";
			script += "	var selectedLinks = new Array();\n";
			script += "\n";
			for (int i = 0; i < ModuleArticle1.SelectedItems.Count; i++)
			{
				ModuleArticleId id = ModuleArticle1.SelectedItems[i];

				Article article = Articles.GetItem(id.ArticleId);
				//Module.Module module = Modules.GetItem(id.ModuleId);
				if (article == null) continue;
				//if (module == null) continue;

				string contentUrl = Hxsoft.AppV1.WebUI.GenericUrlManager.BuildArticleDetailUrl(id.ModuleId, id.ArticleId);

				//string contentUrl = ApplicationConfiguration.GetContentUrl(module.SiteId, module.ModuleCode);
				//contentUrl = contentUrl.Replace("{ModuleId}", module.ModuleId.ToString());
				//contentUrl = contentUrl.Replace("{ModuleCode}", module.ModuleCode);
				//contentUrl = contentUrl.Replace("{ArticleId}", article.ArticleId.ToString());

				script += "	selectedLinks[" + i + "] = new Object();\n";
				script += "	selectedLinks[" + i + "].src = \"" + StringExtensions.ToJavascriptString(contentUrl) + "\";\n";
				script += "	selectedLinks[" + i + "].text = \"" + StringExtensions.ToJavascriptString(article.Title) + "\";\n";
			}
			script += "	SetRelativeLink(selectedLinks);\n";
			script += "-->\n";
			script += "</script>\n";

			ScriptManager.RegisterStartupScript(this, this.GetType(), "SetRelativeLink", script, false);
		}
	}
}
